package com.youkeyi.ddy.cloud.basic.storage.in.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.storage.in.entity.StoStockIn;
import com.youkeyi.ddy.cloud.basic.storage.in.service.StoStockInService;
import com.youkeyi.ddy.cloud.common.api.basic.storage.in.StorageStockInCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.storage.in.dto.StorageStockInListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.storage.in.dto.StorageStockInListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.storage.in.param.StorageStockInCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.in.param.StorageStockInDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.in.param.StorageStockInQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.storage.in.param.StorageStockInUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 入库 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-03-26
 */
@Service
public class StoStockInController implements StorageStockInCloudService {

    @Autowired
    private StoStockInService stoStockInService;

    @Override
    public StorageStockInListCloudDto query(StorageStockInQueryCloudParam param) {
        StorageStockInListCloudDto dto = new StorageStockInListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getStockInPrimaryKeys())) {
            param.getStockInPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getBarCodes())) {
            param.getBarCodes().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getBillNos())) {
            param.getBillNos().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getGoodsKeys())) {
            param.getGoodsKeys().removeAll(Collections.singleton(null));
        }

        QueryWrapper<StoStockIn> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getStockInPrimaryKeys()), StoStockIn::getPrimaryKey, param.getStockInPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), StoStockIn::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getBarCodes()), StoStockIn::getBarCode, param.getBarCodes())
                .in(CollectionUtils.isNotEmpty(param.getBillNos()), StoStockIn::getBillNo, param.getBillNos())
                .in(CollectionUtils.isNotEmpty(param.getGoodsKeys()), StoStockIn::getGoodsKey, param.getGoodsKeys())
                .like(StringUtils.isNotBlank(param.getGoodsNameFuzzy()), StoStockIn::getGoodsName, param.getGoodsNameFuzzy())
                .like(StringUtils.isNotBlank(param.getKeyWordFuzzy()), StoStockIn::getKeyWord, param.getKeyWordFuzzy())
        ;

        Page<StoStockIn> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<StoStockIn> stockInList = stoStockInService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(stockInList.getRecords())) {
            dto.setTotal(stockInList.getTotal());

            List<StorageStockInListItemCloudDto> items = new ArrayList<>();
            stockInList.getRecords().forEach(stockIn -> {
                StorageStockInListItemCloudDto item = new StorageStockInListItemCloudDto();
                item.setStockInPrimaryKey(stockIn.getPrimaryKey());
                item.setCorpKey(stockIn.getCorpKey());
                item.setBillNo(stockIn.getBillNo());
                item.setWarehouseKey(stockIn.getWarehouseKey());
                item.setWarehouseName(stockIn.getWarehouseName());
                item.setBarCode(stockIn.getBarCode());
                item.setGoodsKey(stockIn.getGoodsKey());
                item.setGoodsName(stockIn.getGoodsName());
                item.setKeyWord(stockIn.getKeyWord());
                item.setUnitKey(stockIn.getUnitKey());
                item.setUnitName(stockIn.getUnitName());
                item.setProductDate(stockIn.getProductDate() != null ? stockIn.getProductDate().getTime() : null);
                item.setExpirationDate(stockIn.getExpirationDate() != null ? stockIn.getExpirationDate().getTime() : null);
                item.setShelfKey(stockIn.getShelfKey());
                item.setQuantity(stockIn.getQuantity());
                item.setPrice(stockIn.getPrice());
                item.setAdvicePrice(stockIn.getAdvicePrice());
                item.setSupplierKey(stockIn.getSupplierKey());
                item.setSupplierName(stockIn.getSupplierName());
                item.setInStoreType(stockIn.getInStoreType());
                item.setState(stockIn.getState());
                item.setCooperationMode(stockIn.getCooperationMode());
                item.setOrigStockKey(stockIn.getOrigStockKey());
                item.setNotes(stockIn.getNotes());
                item.setPurchaseUserKey(stockIn.getPurchaseUserKey());
                item.setStorageUserKey(stockIn.getStorageUserKey());
                item.setStorageTime(stockIn.getStorageTime() != null ? stockIn.getStorageTime().getTime() : null);
                item.setFromType(stockIn.getFromType());
                item.setPayType(stockIn.getPayType());
                item.setCreateTime(stockIn.getCreateTime() != null ? stockIn.getCreateTime().getTime() : null);
                item.setCreateUserKey(stockIn.getCreateUserKey());
                item.setCreateUserName(stockIn.getCreateUserName());
                item.setUpdateTime(stockIn.getUpdateTime() != null ? stockIn.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(stockIn.getUpdateUserKey());
                item.setUpdateUserName(stockIn.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(StorageStockInCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getGoodsKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        StoStockIn stockIn = new StoStockIn();
        stockIn.setPrimaryKey(SimpleUUidUtils.getUuid());
        stockIn.setCorpKey(param.getCorpKey());
        stockIn.setBillNo(param.getBillNo());
        stockIn.setWarehouseKey(param.getWarehouseKey());
        stockIn.setWarehouseName(param.getWarehouseName());
        stockIn.setBarCode(param.getBarCode());
        stockIn.setGoodsKey(param.getGoodsKey());
        stockIn.setGoodsName(param.getGoodsName());
        stockIn.setKeyWord(param.getKeyWord());
        stockIn.setUnitKey(param.getUnitKey());
        stockIn.setUnitName(param.getUnitName());
        stockIn.setProductDate(param.getProductDate() != null ? new Date(param.getProductDate()) : null);
        stockIn.setExpirationDate(param.getExpirationDate() != null ? new Date(param.getExpirationDate()) : null);
        stockIn.setShelfKey(param.getShelfKey());
        stockIn.setQuantity(param.getQuantity());
        stockIn.setPrice(param.getPrice());
        stockIn.setAdvicePrice(param.getAdvicePrice());
        stockIn.setSupplierKey(param.getSupplierKey());
        stockIn.setSupplierName(param.getSupplierName());
        stockIn.setInStoreType(param.getInStoreType());
        stockIn.setState(param.getState());
        stockIn.setCooperationMode(param.getCooperationMode());
        stockIn.setOrigStockKey(param.getOrigStockKey());
        stockIn.setNotes(param.getNotes());
        stockIn.setPurchaseUserKey(param.getPurchaseUserKey());
        stockIn.setStorageUserKey(param.getStorageUserKey());
        stockIn.setStorageTime(param.getStorageTime() != null ? new Date(param.getStorageTime()) : null);
        stockIn.setFromType(param.getFromType());
        stockIn.setPayType(param.getPayType());
        stockIn.setCreateUserKey(param.getCreateUserKey());
        stockIn.setCreateUserName(param.getCreateUserName());
        stockIn.setCreateTime(new Date());
        boolean save = stoStockInService.save(stockIn);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(stockIn.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(StorageStockInUpdateCloudParam param) {
        return null;
    }

    @Override
    public ResponseContent delete(StorageStockInDeleteCloudParam param) {
        return null;
    }
}
